package logik;
import gui.Screen;

import java.text.NumberFormat;
import java.util.Calendar;

import data.BankDatabase;

public class InterestRate extends Transaction {

	private double interestRate;
	private double amount;
	private Keypad keypad;
	private Screen screen;
	private int stop = 1;
	int currentYear = Calendar.getInstance().get(Calendar.YEAR); // Gets the current year from the computer
	private BankDatabase bdb;
	private String accountType;
	private NumberFormat nf = NumberFormat.getInstance();

	public InterestRate(int userAccountNumber, Screen atmScreen, 
			BankDatabase atmBankDatabase, String accountType ){

		super(userAccountNumber, atmScreen,atmBankDatabase);

		this.bdb = atmBankDatabase;
		this.screen = atmScreen;
		this.accountType = accountType;
		this.amount = bdb.getTotalBalance(userAccountNumber);
		nf.setMaximumFractionDigits(2);	
	}

	private double getAmount(){
		return amount;
	}

	public void setInterestRate(String accountType){
		if(accountType.equals("Kredit")){
			this.interestRate = 0.0125;
		}
		else if(accountType.equals("Opsparing")){
			this.interestRate = 0.0250;
		}
		if(accountType.equals("BSU")){
			this.interestRate = 0.0450;
		}
		
	}

	public double getInterestRate(){
		return interestRate;
	}

	/* (non-Javadoc)
	 * @see logik.Transaction#execute()
	 */
	
	@Override
	public void execute(){
		setInterestRate(accountType);
		screen.displayMessageLine("\nInterest rate calculater \n\nHere, you can calculate your future balance");
		while(stop == 1){
			screen.displayMessageLine("\nEnter a future year \ne.g. 2022");
			int endyear = keypad.getInput(17); //Prints empty error message since it's already build in. Input skal være fra 2012 og op
			if(endyear < currentYear){
				screen.displayMessageLine("Invalid input, You must enter an integer from " + currentYear + " and above");
			}
			if(endyear >= currentYear){
				screen.displayMessageLine("Year" + "\t" + "Annual Balance");
			}
			for(int year = currentYear; year<=endyear; year++){
				double balance = getAmount() * Math.pow(1 + getInterestRate(), year-currentYear);
				screen.displayMessageLine(year + "\t" + nf.format(balance));
			}
			while(stop != 2){
				screen.displayMessageLine("\nHow do you want to proceed? \n 1: Try Again \n 2: Go back to Menu ");
				stop = keypad.getInput(17);//Prints empty error message since it's already build in
				if(stop == 2){
					screen.displayMessageLine("You have chosen Account Menu");
				}else if(stop > 2 || stop < 1){
					screen.displayMessageLine("Invalid input, You must enter either 1 or 2"); 
				}
				else{
					break;
				} // end else statement
			} // end second while
		} // end first while
	} // end method

}